package com.zx.deploy.common.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;

public class PackUtil {
	public static void zipFiles(File srcfile, File zipfile) {
		byte[] buf = new byte[1024];
		try {
			ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));
			FileInputStream in = new FileInputStream(srcfile);
			out.putNextEntry(new ZipEntry(srcfile.getName()));
			String str = srcfile.getName();
			int len;
			while ((len = in.read(buf)) > 0) {
				out.write(buf, 0, len);
			}
			out.closeEntry();
			in.close();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void unZipFiles(File zipfile, String descDir) throws Exception {
		ZipFile zf = new ZipFile(zipfile);
		for (Enumeration entries = zf.getEntries(); entries.hasMoreElements();) {
			ZipEntry entry = (ZipEntry) entries.nextElement();
			String zipEntryName = entry.getName();
			InputStream in = zf.getInputStream(entry);
			File outFile=new File(descDir + zipEntryName);
			File fileParent = outFile.getParentFile();  
			if(!fileParent.exists()){  
			    fileParent.mkdirs();  
			}  
			outFile.createNewFile();  
			OutputStream out = new FileOutputStream(outFile);
			byte[] buf1 = new byte[1024];
			int len;
			while ((len = in.read(buf1)) > 0) {
				out.write(buf1, 0, len);
			}
			in.close();
			out.close();
		}
	}

	public static void unGzipFiles(File srcfile, String destfilePath) throws Exception {
		GZIPInputStream gzin = new GZIPInputStream(new FileInputStream(srcfile));
		FileOutputStream fout = new FileOutputStream(destfilePath);
		byte[] buf = new byte[1024];
		int len;
		while ((len = gzin.read(buf)) > 0) {
			fout.write(buf, 0, len);
		}
		gzin.close();
		fout.close();
	}

	public static void gzipFiles(File srcfile, File destfile) throws Exception {
		GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(destfile));
		InputStream in = new FileInputStream(srcfile);
		byte[] buf = new byte[1024];
		int len;
		while ((len = in.read(buf)) > 0) {
			out.write(buf, 0, len);
		}
		in.close();
		out.finish();
		out.close();
	}


}
